package com.firebird.app.fekpsl.ui.activity.me

import android.view.View
import com.firebird.app.fekpsl.R
import com.firebird.app.fekpsl.base.BaseMvpActivity
import com.firebird.app.fekpsl.constant.Constant
import com.firebird.app.fekpsl.ext.showToast
import com.firebird.app.fekpsl.mvp.me.NiceNameContract
import com.firebird.app.fekpsl.mvp.me.NiceNamePresenter
import com.firebird.app.fekpsl.utils.DialogUtil
import com.firebird.app.fekpsl.utils.Preference
import kotlinx.android.synthetic.main.me_update_nice_name.*
import kotlinx.android.synthetic.main.toolbar.*

class UpdateNiceNameActivity  : BaseMvpActivity<NiceNameContract.View, NiceNameContract.Presenter>(), NiceNameContract.View {
    private var pf_nicename:String by Preference(Constant.NICE_NAME_KEY, "")
    private var pf_uuid:String by Preference(Constant.UUID_KEY, "")

    override fun createPresenter(): NiceNameContract.Presenter = NiceNamePresenter()

    private val mDialog by lazy {
       // DialogUtil.getWaitDialog(this, getString(R.string.save_ing))
    }

    override fun showLoading() {
       // mDialog.show()
    }

    override fun hideLoading() {
       // mDialog.dismiss()
    }

    override fun updateNiceNameSuccess(success: Boolean) {
        if(success){
            pf_nicename=et_me_nice_name.text.toString()
            finish()
        }else{
           // showToast(getString(R.string.me_save_fail))
        }
    }
/*
    override fun updateNiceNameFail() {
        showToast(getString(R.string.me_save_fail))
    }*/
    override fun attachLayoutRes(): Int = R.layout.me_update_nice_name

    override fun initData() {
    }

    override fun initView() {
        super.initView()

        toolbar.run {
            title = resources.getString(R.string.me_update_nice_name)
            setSupportActionBar(this)
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }
        et_me_nice_name.setText(pf_nicename)
        btn_save.setOnClickListener(onClickListener)
    }

    override fun start() {
    }

    /**
     * OnClickListener
     */
    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.btn_save -> {
                updateNiceName()
            }
        }
    }
    private fun updateNiceName() {
        if (validate()) {
            mPresenter?.updateNiceName(pf_uuid,
                    et_me_nice_name.text.toString()
            )
        }
    }
    /**
     * check data
     */
    private fun validate(): Boolean {
        var valid = true
        val niceName: String = et_me_nice_name.text.toString()

        if (niceName.isEmpty()) {
            et_me_nice_name.error = getString(R.string.me_nice_name_not_empty)
            valid = false
        }
        if (niceName.length>20){
            et_me_nice_name.error = getString(R.string.me_nice_name_len_error)
            valid = false
        }
        return valid
    }

}